capture log close
clear all
set maxvar 10000
set more off
pause off
# delimit;

/* 
Figure B12
*/

local data "" ;
local output "" ;


/* Means for crimeXemp */
use "prwora_cXe_summstats.dta", clear ;
drop 	
	n_crime2nogn_1997_2017* 
	n_crime2nognt_1997_2017* ;
gen id = _n ;
reshape long 
	n_crime2emp5c_1997_2017 
	n_crime2emp5g_1997_2017 
	n_crime2emp5i_1997_2017 
	n_crime2emp5ct_1997_2017 
	n_crime2emp5gt_1997_2017 
	n_crime2emp5it_1997_2017 
	n_crime2emp15c_1997_2017 
	n_crime2emp15g_1997_2017 
	n_crime2emp15i_1997_2017 
	n_crime2emp15ct_1997_2017 
	n_crime2emp15gt_1997_2017 
	n_crime2emp15it_1997_2017 
	n_crimeno2emp5c_1997_2017 
	n_crimeno2emp5g_1997_2017 
	n_crimeno2emp5i_1997_2017 
	n_crimeno2emp5ct_1997_2017 
	n_crimeno2emp5gt_1997_2017 
	n_crimeno2emp5it_1997_2017 
	n_crimeno2noe5c_1997_2017 
	n_crimeno2noe5g_1997_2017 
	n_crimeno2noe5i_1997_2017 
	n_crimeno2noe5ct_1997_2017 
	n_crimeno2noe5gt_1997_2017 
	n_crimeno2noe5it_1997_2017 
	n_crimeno2emp15c_1997_2017 
	n_crimeno2emp15g_1997_2017 
	n_crimeno2emp15i_1997_2017 
	n_crimeno2emp15ct_1997_2017 
	n_crimeno2emp15gt_1997_2017 
	n_crimeno2emp15it_1997_2017 
	n_crimeno2noe15c_1997_2017 
	n_crimeno2noe15g_1997_2017 
	n_crimeno2noe15i_1997_2017 
	n_crimeno2noe15ct_1997_2017 
	n_crimeno2noe15gt_1997_2017 
	n_crimeno2noe15it_1997_2017 
	n_crime2noemp5c_1997_2017 
	n_crime2noemp5g_1997_2017 
	n_crime2noemp5i_1997_2017 
	n_crime2noemp5ct_1997_2017 
	n_crime2noemp5gt_1997_2017 
	n_crime2noemp5it_1997_2017 
	n_crime2noemp15c_1997_2017 
	n_crime2noemp15g_1997_2017 
	n_crime2noemp15i_1997_2017 
	n_crime2noemp15ct_1997_2017 
	n_crime2noemp15gt_1997_2017 
	n_crime2noemp15it_1997_2017
, i(id) j(num) ;

drop id ;
reshape long 
	n_crime2emp n_crimeno2emp n_crimeno2noe n_crime2noemp
	, i(num) j(stub) string ;
rename n_crime2emp mean_cyesXeyes ;
rename n_crimeno2emp mean_cnoXeyes ;
rename n_crimeno2noe mean_cnoXeno ; 
rename n_crime2noemp mean_cyesXeno ;

local cXe_list "all remhigh remlow ueallhigh uealllow crimehigh crimelow pophigh poplow demhigh demlow 
parhigh parlow male female singno twopar othmen nonment intel phys white black nonw" ;

local demo_ct = 0 ;
foreach demo in `cXe_list' { ;
	local demo_ct = `demo_ct' + 1 ;
	local ct_`demo' = `demo_ct' ;
} ;	/* end demo */

gen sample = "" ;
foreach demo in `cXe_list' { ;
	replace sample = "`demo'" if num == `ct_`demo'' ;
} ;

order sample ;
tempfile means ;
save `means' ;


local genr_first "g" ;
local genr_title "income-gen charge" ;
local genr_yelabel "Crime" ;
local genr_yilabel "More crime" ;
local genr_nelabel "No crime" ;
local genr_nilabel "Less crime" ;

local e_title "extensive" ;
local i_title "intensive" ;

local c_title  ", 1997-2017" ;

local mult_age18unfav= 1 ;

local ylab_age18unfav "-.2(.1).2" ;

local margin_i "t" ;

foreach sample in all remhigh remlow ueallhigh uealllow crimehigh crimelow pophigh poplow demhigh demlow 
parhigh parlow male female singno twopar othmen nonment intel phys white black nonw {;
foreach time in c { ;
	forval amt = 15(5)15 { ;
		foreach type in genr  { ;
			foreach margin in i { ;
				foreach endo in age18unfav { ;
		
				use "prwora_cXe_bw264_crime_poly1allcov_iv_`sample'.dta", clear ;
				
				duplicates drop variables, force ;
			
				keep if 
					(variables == "n_`time'rime2emp`amt'``type'_first'`margin_`margin''_1997_2017" |
					 variables == "n_`time'rime2noemp`amt'``type'_first'`margin_`margin''_1997_2017" |
					 variables == "n_`time'rimeno2emp`amt'``type'_first'`margin_`margin''_1997_2017" |
					 variables == "n_`time'rimeno2noe`amt'``type'_first'`margin_`margin''_1997_2017")
					;
					
				replace variables = "cnoXeyes" 	if variables == "n_`time'rimeno2emp`amt'``type'_first'`margin_`margin''_1997_2017" ;
				replace variables = "cnoXeno"  	if variables == "n_`time'rimeno2noe`amt'``type'_first'`margin_`margin''_1997_2017" ;
				replace variables = "cyesXeyes" if variables == "n_`time'rime2emp`amt'``type'_first'`margin_`margin''_1997_2017" ;
				replace variables = "cyesXeno" 	if variables == "n_`time'rime2noemp`amt'``type'_first'`margin_`margin''_1997_2017" ;
				
				keep variables n_age18unfav ;
				gen id = 1 ;
				gen num = 1 if variables == "cnoXeyes" ;
				replace num = 2 if variables == "cnoXeno" ;
				replace num = 3 if variables == "cyesXeyes" ;
				replace num = 4 if variables == "cyesXeno" ;
				drop variables ;
				reshape wide n_age18unfav, i(id) j(num) ;
				rename n_age18unfav1 cnoXeyes ;
				rename n_age18unfav2 cnoXeno ;
				rename n_age18unfav3 cyesXeyes ;
				rename n_age18unfav4 cyesXeno ;
				
				gen crimeonly_share = -cyesXeno/cnoXeno ;
				gen emponly_share = -cnoXeyes/cnoXeno ;
				gen crimeemp_share = -cyesXeyes/cnoXeno ;
				
				keep crimeonly_share emponly_share crimeemp_share cyesXeno cnoXeyes cyesXeyes ;
				gen sample = "`sample'" ;
				gen time = "`time'" ;
				gen amt = `amt' ;
				gen type = "`type'" ;
				gen margin = "`margin'" ;
				pause ;
				tempfile `sample'_`time'_`amt'_`type'_`margin' ;
				save ``sample'_`time'_`amt'_`type'_`margin'' ;
					
				} ; /* end endo */
			} ; /* end margin */
		} ; /* end type */
	} ; /* end amt */
} ; /* end time */
} ; /* end sample */

use `all_c_15_genr_i', clear ;
foreach sample in all remhigh remlow ueallhigh uealllow crimehigh crimelow pophigh poplow demhigh demlow 
parhigh parlow male female singno twopar othmen nonment intel phys white black nonw {;
foreach time in c { ;
	forval amt = 15(5)15 { ;
		foreach type in genr  { ;
			foreach margin in i { ;
				foreach endo in age18unfav { ;
				
					append using ``sample'_`time'_`amt'_`type'_`margin'' ;
					
				} ; /* end endo */
			} ; /* end margin */
		} ; /* end type */
	} ; /* end amt */
} ; /* end time */
} ; /* end sample */	
duplicates drop ; 		

tostring amt, gen(amt_concat) ;
gen type_concat = "g" if type == "genr" ;
replace type_concat = "i" if type == "inca" ;
replace type_concat = "c" if type == "char" ;
gen margin_concat = "" if margin == "e" ;
replace margin_concat = "t" if margin == "i" ;
gen time_concat = "_1997_2017" if time == "c" ;
gen stub = amt_concat + type_concat + margin_concat + time_concat ;

merge 1:1 sample stub using `means', keepusing(mean_*) ;
drop if _merge == 2 ;
drop _merge ;

capture drop sort ;
gen 	sort = 17 if sample == "all" ;
replace sort = 16 if sample == "female" ;
replace sort = 15 if sample == "crimehigh" ;
replace sort = 14 if sample == "ueallhigh" ;
replace sort = 13 if sample == "poplow" ;
replace sort = 12 if sample == "phys" ;
replace sort = 11 if sample == "black" ;
replace sort = 10 if sample == "intel" ;
replace sort = 9 if sample == "parlow" ;
replace sort = 8 if sample == "twopar" ;
replace sort = 7 if sample == "singno" ;
replace sort = 6 if sample == "parhigh" ;
replace sort = 5 if sample == "pophigh" ;
replace sort = 4 if sample == "othmen" ;
replace sort = 3 if sample == "male" ;
replace sort = 2 if sample == "uealllow" ;
replace sort = 1 if sample == "white" ;
replace sort = 0 if sample == "crimelow" ;


twoway 				
	(scatter sort cyesXeno , color(red*1.5) msymbol(X) msize(large))
	(scatter sort cnoXeyes , color(green*1.5) msymbol(T))
	(scatter sort cyesXeyes , color(red*.75) msymbol(d) msize(small))
	,
	legend(col(3) row(1) order(1 "Crime only" 2 "Employment only" 3 "Both" ))
	graphregion(color(white))
	xtitle("IV estimate of response to SSI removal", size(medsmall))
	bgcolor(white)
	ylabel(
		   17 "All" 16 "Female"  15 "High crime area" 14 "High unemp area"  
		   13 "Rural" 12 "Physical" 11 "Black" 10 "Intellectual"
		   9 "Low par earn" 8 "Two parents" 7 "Single/no par" 
		   6 "High par earn" 5 "Urban" 4 "Other mental" 3 "Male" 
		   2 "Low unemp area" 1 "White" 0 "Low crime area", 
		   angle(0) notick labsize(small) ) 
	ytitle("")
	title("", size(medsmall))
	xlabel(,nogrid)
	;
graph export "`output'/figB12.png", width(1000) height(750) replace ;

